<?php
class Adm extends CI_Controller{
	var $data = array();
	var $ionuser;
	function testipaddr(){
		echo $_SERVER['HTTP_CLIENT_IP'] . '<br />';
		foreach ($_SERVER as $x=>$y){
			echo $x . ' => ' . $y . '<br />';
		}
	}
	function createdate(){
		echo date('d/m/Y');
	}
	function addalert(){
		$params = $this->input->post();
		$obj = $params;
		unset($obj['group']);
		unset($obj['sender']);
		foreach(User::get_user_by_group($params['group']) as $user){
			$obj['targetuser'] = $user->username;
			Alert::add($obj);
		}
	}
	function addmessage(){
		$params = $this->input->post();
		$obj = $params;
		unset($obj['group']);
		foreach(User::get_user_by_group($params['group']) as $user){
			$obj['targetuser'] = $user->username;
			Message::add($obj);
		}
	}
	function addotherclient(){
		$params = $this->input->post();
		echo Survey_client_distance::survey_client_add($params);
	}
	function addrouter(){
		$params = $this->input->post();
		echo Install_router::add_router($params);
	}
	function survey_addsite(){
		$params = $this->input->post();
		echo Survey_site::site_add($params);
	}
	function addsurveyimage(){
		$params = $this->input->post();
		echo Survey_image::add($params);
	}
	function addaw(){
		$params = $this->input->post();
		echo Install_ap_wifi::add_aw($params);
	}
	function addinstall(){
		$params = $this->input->post();
		echo Install_request::add($params);
	}
	function addinstalldate(){
		$params = $this->input->post();
		echo Install_date::add($params);
	}
	function addrw(){
		$params = $this->input->post();
		echo Install_wireless_radio::add_wr($params);
	}
	function addsurveydate(){
		$params = $this->input->post();
		echo Survey_date::add($params);
	}
	function backbones(){
		$this->check_login();
		$this->data['objs'] = Backbone::populate();
		$this->data['menuFeed'] = 'backbone';
		$this->load->view('adm/backbones',$this->data);
	}
	function btses(){
		$this->check_login();
		$this->data['objs'] = Btstower::populate();
		$this->data['menuFeed'] = 'bts';
		$this->load->view('adm/btses',$this->data);
	}
	function addsurveysite(){
		$params = $this->input->post();
		Survey_site::site_add($params);
	}
	function addsurveydevice(){
		$params = $this->input->post();
		echo Survey_device::add($params);
	}
	function addsurveymaterial(){
		$params = $this->input->post();
		echo  Survey_material::add($params);
	}
	function addtopologijaringan(){
		$params = $this->input->post();
		echo Install_image::add_image($params);
	}
	function aps(){
		$this->check_login();
		$this->data['objs'] = Ap::populate();
		$this->data['btses'] = Btstower::get_combo_data();
		$this->data['menuFeed'] = 'ap';
		$this->load->view('adm/aps',$this->data);
	}
	function btsdistance_remove(){
		$params = $this->input->post();
		Survey_bts_distance::remove_by_id(array($params['btsdistance_id']));
		echo $params['btsdistance_id'];
	}
	function __construct(){
		parent::__construct();
		$this->load->library('ion_auth');
		$this->load->helper('language');
		if($this->ion_auth->logged_in()){
			$this->ionuser = $this->ion_auth->user()->row();
			$this->data['user'] = User::get_user_by_id($this->ionuser->id);
		}
	}
	function change_password(){
		$params = $this->input->post();
		$this->ion_auth_model->change_password($params['email'],$params['oldpassword'],$params['password']);
	}
	function check_login(){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url() . 'adm/login');
		}
	}
	function client_lookup(){
		$this->check_login();
		switch($this->uri->segment(3)){
			case 'add-install-lookup':
				$objs = Survey_request::populate(1);
				$this->data['objs']=$objs->client;
				$this->data['return_page']='adm/install_add/';
			break;
			default:
				$this->data['objs']=Client::populate();
			break;
		}
		$this->load->view('adm/client_lookup',$this->data);
	}
	function closesurveydate(){
		$params = $this->input->post();
		echo Survey_request::close_survey_date($params);
	}
	function closeinstalldate(){
		$params = $this->input->post();
		echo Install_request::close_install_date($params);
	}
	function createuser(){
		$this->check_login();
		$this->data['obj'] = User::populate();
		$this->load->view('adm/createuser',$this->data);
	}
	function dashboard(){
		$this->check_login();
		$this->data['menuFeed'] = 'dashboard';
		$this->load->view('adm/pujicalendar',$this->data);
	}
	function d_calendar(){
		$year = date('Y');
		$month = date('m');
		$arr = array();
		foreach(Survey_request::get_survey_requests('0') as $req){
			$mydt = Common::longsql_to_datepart($req->survey_date);
			array_push($arr,array('title'=>$req->client->name,'start'=>$req->survey_date));
		}
		array_push($arr,array(
		'title'=>'hola','start'=>'2014-06-02 13:16:28',
		'end'=>'2014-06-03 13:16:28',
		'agenda'=>'8:45',
		'allDaySlot'=>false,
		'url'=>base_url() . 'adm/dashboard',
		'color'=>'red'
		));
		array_push($arr,array(
		'title'=>'mbahmu',
		'start'=>'2014-06-08 11:16:28',
		'end'=>'2014-06-10 10:16:28',
		'allDay'=>false,
		'url'=>base_url() . 'adm/dashboard',
		'color'=>'red'
		));
		array_push($arr,array(
		'title'=>'hola2','start'=>'2014-06-02 14:16:28',
		'end'=>'2014-06-02 15:16:28',
		'agenda'=>'12:45',
		'url'=>base_url() . 'adm/dashboard',
		'color'=>'green'
		));
		echo json_encode($arr);
	}
	function deactivealert(){
		$params = $this->input->post();
		echo Alert::deactivealert($params);
	}
	function deactivemessage(){
		$params = $this->input->post();
		echo Message::deactivemessage($params);
	}
	function deviceupdate(){
		$params = $this->input->post();
		echo Device::edit($params);
	}
	/*function deviceremove(){
		$params = $this->input->post();
		echo Device::remove($params['id']);
	}*/
	function differenceintimes(){
		$params = $this->input->post();
		$result = Common::differenceInTimes($params['starttime'],$params['endtime']);
		echo "$result->d hari, $result->h jam, $result->i menit";
	}
	function differenceintimes2(){
		$params = $this->input->post();
		$result = Common::differenceInTimes($params['starttime'],$params['endtime']);
		if($result->d==0){
			if($result->h==0){
				if($result->i<=30){
					echo "< 30 menit";
				}else{
					echo ">30 menit <= 1 jam";
				}
			}
			elseif($result->h==1){
				if($result->i==0){
					echo ">30 menit <= 1 jam";
				}else{
					echo "1 - <= 2 jam";
				}
			}
			elseif($result->h==2){
				if($result->i==0){
					echo "1 - <= 2 jam";
				}else{
					echo "2 - <=4 jam";
				}
			}else{
				echo "4 - <=24 jam";
			}
		}
		if($result->d==1){
			if($result->h==1){
				if($result->i==0){

				}
			}
		}
	}
	function fprospecttosurvey(){
		$cl = new Client();
		$cl->where('id',$this->uri->segment(3))->get();
		$sr = new Survey_request();
		$sr->save($cl);
		redirect(base_url() . 'adm/prospects/');
	}
	function get_account(){
		echo '{"name":"Jojon","group":"Sales"}';
	}
	function get_alerts(){
		if($this->ion_auth->logged_in()){
			echo Message::get_messages($this->session->userdata['username']);
		}else{
			echo 'false';
		}
	}
	function testdate2(){
		$dates = Common::daysbetween('2/10/1993','2/11/1993');
		foreach($dates as $dt){
			echo $dt . '<br />';
		}
	}
	function testdate3(){
		foreach(getdate() as $k=>$v){
			echo $k . ' and ' . $v . '<br />';
		}
	}
	function testdate(){
		echo strtotime('1/11/2013') . '<br />';
		echo 'Maketime ' . mktime(0,0,0,2,23,2013) . '<br />';
		echo 'Maketime ' . date('d-m-Y',mktime(0,0,0,2,23,2013)) . '<br />';
		$date1=date_create("2013-12-15");
		echo 'date create --> ' . $date1->format("Y m d H a y s") . ' <-- datecreate <br />';
		$date2=date_create("2013-12-12");
		$diff=date_diff($date1,$date2);		//$this->datess('','');
		echo $diff->format("%R%a days") . '<br />';
		$dates = $this->datess("10/12/2013","15/12/2013");
		echo 'Tanggal baru';
		echo datediff($date1,getdate())->format('d');
	}
	function ticketadd(){
		$params = $this->input->post();
		echo Ticket::addticket($params);
	}
	function ticketupdate(){
		$params = $this->input->post();
		echo Ticket::updateticket($params);
	}
	function chooseRole(){
		$this->check_login();
		$user = new User();
		$user->where('id',$this->session->userdata['user_id'])->get();
		if($user->group->result_count()>1){
			$data = array("roles"=>$user->group);
			$this->load->view("adm/chooseRole",$data);
		}else{
			switch($user->group->name){
				case "TS":
					$session_data = array(
						'role'=>"TS"
					);
					$this->session->set_userdata($session_data);						
					redirect (base_url() . "tickets");
				break;
				case "Sales":
					$session_data = array(
						'role'=>"Sales"
					);
					$this->session->set_userdata($session_data);						
					redirect (base_url() . "surveys");
				break;
				case "Umum dan Warehouse":
					$session_data = array(
						'role'=>"Umum dan Warehouse"
					);
					$this->session->set_userdata($session_data);						
					redirect (base_url() . "adm/devicetypes");
				break;
				case "Administrator":
					$session_data = array(
						'role'=>"Administrator"
					);
					$this->session->set_userdata($session_data);						
					redirect (base_url() . "tickets");
				break;
			}
		}
	}
	function datess($start,$end){
		$date1 = explode('/',$start);
		$date2 = explode('/',$end);
		$dttm1 = date_create($date1[2]. '-' . $date1[1] . '-' . $date1[0]);
		$dttm2 = date_create($date2[2]. '-' . $date2[1] . '-' . $date2[0]);
		$diff = date_diff($dttm1,$dttm2);
		$tgl = $date1[0];
		$startdate = date("Y-m-d",mktime(0,0,0,$date1[1],$tgl,$date1[2]));
		$out = array();
		echo $diff->format("%R%a hari") . '<br />';
		for($i=0;$i<$diff->format("%a");$i++){
			$tmpdate = date("Y-m-d",mktime(0,0,0,$date1[1],$tgl+$i,$date1[2]));
			echo $tmpdate . '<br />';
		}
	}
	function devices(){
		$this->check_login();
		$this->data['objs'] = Device::populate();
		$this->data['menuFeed'] = 'device';
		$this->data['devicetypes'] = Devicetype::get_combo_data();
		$this->load->view('adm/devices',$this->data);
	}
	function devicetypeadd(){
		$params = $this->input->post();
		echo Devicetype::add($params);
	}
	function devicetypes(){
		$this->check_login();
		$this->data['objs'] = Devicetype::populate();
		$this->data['menuFeed'] = 'devicetype';
		$this->load->view('adm/devicetypes',$this->data);
	}
	function getdevice(){
		$params = $this->uri->segment(3);
		//Devicetype::get_combo_data();
		echo array("1"=>"satu");
	}
	function get_model($modelname,$colarray){
		$obj = new $modelname();
		foreach($colarray as $key=>$val){
			$obj->where($key,$val);
		}
		$obj->get();
		return $obj;
	}
	function get_other_sites(){
		$params = $this->input->post();
		echo Survey_site::get_other_sites($params['id']);
	}
	function get_rowcount(){
		$params = $this->input->post();
		$obj = $this->get_model($params["modelname"],array($params["colname"]=>$params["colval"]));
		echo $obj->result_count();
	}
	function get_dashboard(){
		$dt1 = $this->uri->segment(3) . '/' . $this->uri->segment(4) . '/' .$this->uri->segment(5);
		$dt2 = $this->uri->segment(6) . '/' . $this->uri->segment(7) . '/' .$this->uri->segment(8);

		$datesbetween = Common::daysbetween($dt1,$dt2);
		$arr = array();
		foreach($datesbetween as $days){

		$query = " select a.name,b.survey_date survey,";
		$query.= " case when b.survey_date is null then '-' else  a.name end bname,";
		$query.= " case when b.survey_date is null then '-' else   case b.status when '0' then 'belum' when '1' then 'progress' when '2' then 'selesai' end  end bstatus,";
		$query.= " case when b.survey_date is null then '-' else  a.service_interested_to end bservice,";

		$query.= " case when c.install_date is null then '-' else  a.name end cname,";
		$query.= " case when c.install_date is null then '-' else   case c.status when '0' then 'belum' when '1' then 'progress' when '2' then 'selesai' end  end cstatus,";
		$query.= " case when c.install_date is null then '-' else  a.service_interested_to end cservice,";

		$query.= " case when d.maintenance_date is null then '-' else  a.name end dname,";
		$query.= " case when d.maintenance_date is null then '-' else  case d.status when '0' then 'belum' when '1' then 'progress' when '2' then 'selesai' end end dstatus,";
		$query.= " case when d.maintenance_date is null then '-' else  d.description end dproblem,";

		$query.= " c.install_date install,d.maintenance_date maintenance ";
		$query.= " from clients a left outer join ";
		$query.= " (select * from survey_requests where survey_date='" . $days . "') b on a.id=b.client_id left outer join ";
		$query.= " (select * from install_requests where install_date='" . $days . "') c on c.client_id=a.id left outer join ";
		$query.= " (select * from maintenance_requests where maintenance_date='" . $days . "') d on d.client_id=a.id ";
		$query.= " where b.id is not null or c.id is not null or d.id is not null ";

		$result = $this->db->query($query);
			$arr2 = array();
			$c = 1;
			foreach($result->result() as $r){
				$myresult = '"' . $c . '":
				{
				"Survey":{"nama":"' . $r->bname . '","layanan":"' . $r->bservice . '","status":"' . $r->bstatus . '"},
				"Install":{"nama":"' . $r->cname . '","layanan":"' . $r->cservice . '","status":"' . $r->cstatus . '"},
				"Maintenance":{"nama":"' . $r->dname . '","problem":"' . $r->dproblem . '","status":"' . $r->dstatus . '"}
				}';
				array_push($arr2,$myresult);
				$c++;
			}
			$row = '"' . $days . '":{' . implode(",",$arr2) . '}';

			//array_push($arr,$daily);
			array_push($arr,$row);
		}
		$out = '{' . implode(',',$arr) . '}';
		echo $out;
	}
	function get_datacenters(){
		/*$arr = array();
		foreach(Datacenter::get_combo_data() as $key=>$val){
			array_push($arr, '"' . $key . '":"' . $val . '"');
		}
		$out = implode(',',$arr);
		$out = '{' . $out . '}';
		echo $out;
		*/
		echo '{"1":"Telkom","2":"Indosat"}';
		//echo '{"1":"Paragon","2":"Garden","3":"Untag","4":"Menanggal","5":"Hartono","6":"Colors","7":"Trosobo","8":"Swadaya","9":"SMP Muhammadiyah","10":"Raci"}';
	}
	function get_lastvisit(){
		$params = $this->input->post();
		echo App_log::get_lastvisit($params['username']);
	}
	function get_notification(){
		$group = $this->uri->segment(3);
		switch($group){
			case 'none':
				$survey_0 = Survey_request::get_notification('0');
				$survey_1 = Survey_request::get_notification('1');
				$survey_2 = Survey_request::get_notification('2');
				$survey_3 = Survey_request::get_notification('3');

				$install_0 = Install_request::get_notification('0');
				$install_1 = Install_request::get_notification('1');
				$install_2 = Install_request::get_notification('2');
				$install_3 = Install_request::get_notification('3');

				$maintenance_0 = Maintenance_request::get_notification('0');
				$maintenance_1 = Maintenance_request::get_notification('1');
				$maintenance_2 = Maintenance_request::get_notification('2');
				$maintenance_3 = Maintenance_request::get_notification('3');
				echo '{"survey_0":"' . $survey_0 . '","survey_1":"' . $survey_1 . '","survey_2":"' . $survey_2 . '","survey_3":"' . $survey_3 . '","install_0":"' . $install_0 . '","install_1":"' . $install_1 . '","install_2":"' . $install_2 . '","install_3":"' . $install_3 . '","maintenance_0":"' . $maintenance_0 . '","maintenance_1":"' . $maintenance_1 . '","maintenance_2":"' . $maintenance_2 . '","maintenance_3":"' . $maintenance_3 . '"}';
			break;
			case 'sales':
				$survey_2 = Survey_request::get_notification('2');
				$install_2 = Install_request::get_notification('2');
				$maintenance_2 = Maintenance_request::get_notification('2');
				echo '{"survey_2":"' . $survey_2 . '","install_2":"' . $install_2 . '","maintenance_2":"' . $maintenance_2 . '"}';
			break;
			case 'TS':
				$survey_not_read = Survey_request::get_notification('0');
				$survey_not_done = Survey_request::get_notification('1');
				$survey_done = Survey_request::get_notification('5');
				$install_not_read = Install_request::get_notification('0');
				$install_not_done = Install_request::get_notification('1');
				$install_done = Install_request::get_notification('5');
				$maintenance_not_read = Maintenance_request::get_notification('0');
				$maintenance_not_done = Maintenance_request::get_notification('1');
				$maintenance_done = Maintenance_request::get_notification('5');
				echo '{"survey_not_read":"' . $survey_not_read . '","survey_not_done":"' . $survey_not_done . '","survey_done":"' . $survey_done . '","install_not_read":"' . $install_not_read . '","install_not_done":"' . $install_not_done . '","install_done":"' . $install_done . '","maintenance_not_read":"' . $maintenance_not_read . '","maintenance_not_done":"' . $maintenance_not_done . '","maintenance_done":"' . $maintenance_done . '"}';

			break;
		}
	}
	function get_last_id(){
		echo '{"last_id":"' . mysql_insert_id() . '"}';
		//echo '{"last_id":42}';
	}
	function get_prospects(){
		$id = $this->uri->segment(3);
		$obj = new Prospect();
		$obj->where('id',$id)->get();
		echo '{"pic_name":"' . $obj->prospectpic->name . '","pic_phone":"' . $obj->speed . '","pic_mail":"' . $obj->ratio . '"}';
	}
	function getExtension($str)
	{

			 $i = strrpos($str,".");
			 if (!$i) { return ""; }

			 $l = strlen($str) - $i;
			 $ext = substr($str,$i+1,$l);
			 return $ext;
	 }
	function getticket(){
		$id = $this->uri->segment(3);
		$ticket = Ticket::get_ticket($id);
		//echo '{"clientname":"ghi","content":"def","ticketstart":"abc"}';
		echo '{"clientname":"' . $ticket->clientname . '","content":"' . $ticket->content . '","ticketstart":"' . $ticket->ticketstart . '","ticketend":"' . $ticket->ticketend . '","requeststart":"' . $ticket->requeststart . '","requestend":"' . $ticket->requestend . '","requesttype":"' . $ticket->requesttype . '"}';
	}
	function handler(){
		$params = $this->input->post();
		switch ($params['sender']){
			case 'login':
				if($this->ion_auth->login($params['username'],$params['password'])){
					App_log::create_log('login');
					/*$user = new User();
					$user->where('id',$this->session->userdata['id'])->get();
					/*if($user->group->result_count()>1){
						
					}*/
					redirect(base_url() . 'adm/chooseRole');
					echo 'masuk';
				}else{
					redirect(base_url() . 'adm/login/fail');
				}

				break;
			case 'survey_add':
				Survey_request::update($params);
				break;

			case 'survey_edit':
				Survey_request::request_update($params);
				redirect(base_url() . 'adm/surveys');
				break;

			case 'cabangklienadd':
				Client_site::add($params);
				break;
		}
	}
	function index(){
		$this->check_login();
		$data = array(
			"menuFeed"=>"",
		);
		$this->load->view('adm/dashboard',$data);
	}
	function install_act(){
		$this->check_login();
		$this->data['obj']=Install_request::get_obj_by_id($this->uri->segment(3));
		$this->data['clients']=Client::get_combo_data();
		$this->data['sender']='survey_edit';
		$this->data['menuFeed']='install';
		$this->load->view('adm/install_act',$this->data);
	}
	function install_add(){
		$this->check_login();
		$this->data['obj']=Survey_request::get_obj_by_id($this->uri->segment(3));
		$this->data['clients']=Client::get_combo_data();
		$this->data['sender']='install_add';
		$this->load->view('adm/install_add',$this->data);
	}
	function install_addofficer(){
		$params = $this->input->post();
		$user = new User();
		$user->where('id',$params['user_id'])->get();
		$request = new Install_request();
		$request->where('id',$params['request_id'])->get();
		$request->save($user);
	}
	function install_dates(){
		$this->check_login();
		$id = $this->uri->segment(3);
		$obj = Install_request::get_obj_by_id($id);
		$this->data['obj']=$obj;
		//$this->data['check0']=$obj->checkresume(0);
		//$this->data['check1']=$obj->checkresume(1);
		//$this->data['check2']=$obj->checkresume(2);
	//	$this->data['check3']=$obj->checkresume(3);
//		$this->data['clients']=Client::get_combo_data();
		$this->data['sender']='install_dates';
		$this->load->view('adm/install_dates',$this->data);

	}
	function install_addsite(){
		$params = $this->input->post();
		echo Install_site::add($params);
	}
	function install_removesite(){
		$params = $this->input->post();
		echo Install_site::remove_site($params['id']);
	}
	function install_updatesite(){
		$params = $this->input->post();
		echo Install_site::update_site($params);
	}
	function install_site(){
		$this->check_login();
		$id = $this->uri->segment(3);
		$path = array(
			'csspath' => base_url() . 'css/aquarius/',
			'jspath' => base_url() . 'js/aquarius/',
			'imagepath' => base_url() . 'img/aquarius/',
			'user'=>User::get_user_by_id($this->user->id)
			);
		$data = array(
			'aps'=>AP::get_combo_data(),
			'antennas'=>Device::get_combo_data(8),
			'obj'=>Install_site::get_obj_by_install_id($id),
			'btstowers'=>Btstower::get_combo_data(),
			'clients'=>Client::get_combo_data(),
			'devices'=>Device::get_combo_data(),
			'materials'=>Material::get_combo_data(),
			'materialtypes'=>Materialtype::get_combo_data(),
			'path'=>$path,
			'pccards'=>Device::get_combo_data(1),
			'routers'=>Device::get_combo_data(14),
			'apwifis'=>Device::get_combo_data(13),
			'sender'=>'install_edit',
			'officers'=>User::get_user_by_group('TS')
		);
		$data = array_merge($data,$this->data);
		$this->load->view('adm/install_site',$data);
	}
	function install_removerouter(){
		$params = $this->input->post();
		Install_router::remove_router($params['id']);
	}
	function install_removeaw(){
		$params = $this->input->post();
		echo Install_ap_wifi::remove_aw($params['id']);
	}
	function install_removerw(){
		$params = $this->input->post();
		echo Install_wireless_radio::remove_rw($params['id']);
	}
	function install_removetopologijaringan(){
		$params = $this->input->post();
		echo Install_image::remove_image($params['id']);
	}
	function installsiteadd(){
		$params = $this->input->post();
		echo Install_site::add($params);
	}
	function installsite_saveimage(){
		$params = $this->input->post();
		rename('./media/tmp/' . $params['path'],'./media/installs/' . $params['path']);
		echo Install_site::saveimage($params);
	}
	function isonline(){
		$username = $this->uri->segment(3);
		$q = 'select user_data from ci_sessions';
		$d = $this->db->query($q);
		$r = $d->result();
		$out = array();
		foreach($r as $k=>$v){
			foreach($v as $a=>$b){
				if (count($b)>0){
					if(stripos($b,$username)>0){
						echo 'ada';
						return true;
					}
				}
			}
		}
		echo 'tidak ada';
		return false;
	}
	function login(){
		$this->load->view('adm/login',$this->data);
	}
	function logout(){
		echo "logout";
		App_log::create_log('Logout');
		redirect(base_url() . "adm/login");
	}
	function material_remove(){
		$params = $this->input->post();
		$obj = new Survey_material();
		$obj->where('id',$params['id'])->get();
		$obj->delete();
		echo $params['material_id'];
	}

/*************/
	function materials(){
		$this->check_login();
		$this->data['objs'] = Material::populate();
		$this->data['menuFeed'] = 'material';
		$this->data['materialtypes'] = Materialtype::get_combo_data();
		$this->load->view('adm/materials',$this->data);
	}
	function materialremove(){
		$params = $this->input->post();
		echo Material::remove($params["id"]);
	}
	function materialsave(){
		$params = $this->input->post();
		echo Material::add($params);
	}
	function materialtypeadd(){
		$params = $this->input->post();
		echo Materialtype::add($params);
	}
	function materialtypes(){
		$this->check_login();
		$this->data['objs'] = Materialtype::populate();
		$this->data['menuFeed'] = 'materialtype';
		$this->load->view('adm/materialtypes',$this->data);
	}
	function member_of($group){
		if(User::get_group_name($this->ionuser->id)==$group){
			return true;
		}
		return false;
	}
	function messages(){
		$this->check_login();
		$data = array(
			'menuFeed' => 'messages',
			'user'=>User::get_user_by_id($this->user->id)
		);
		$this->load->view('adm/messages',$data);
	}
	function printpdf(){
		$o = new Pdftable();
		$o->SetFont('Arial','I',12);
		$o->AddPage();
		$data = array(
		"nama"=>"Jasa Mitra Propertindo",
		"tipe"=>"Corporate",
		"Sites"=>array(
			"PGS"=>array("name"=>"PGS","location"=>"Pusat Grosir Surabaya"),
			"ITC"=>array("name"=>"ITC","location"=>"International Trade Center"),
			"JMP"=>array("name"=>"JMP","location"=>"Jembatan Merah Plaza"),
			"TEC"=>array("name"=>"TEC","location"=>"Tunjungan Electronic Center"),
			"GFM"=>array("name"=>"GFM","location"=>"Graha Family"),
			"HTJ"=>array("name"=>"HTJ","location"=>"Hotel Tunjungan","images"=>array("programmer.jpg")),
			"RPZ"=>array("name"=>"RPZ","location"=>"Royal Plaza"),
			"GLM"=>array("name"=>"GLM","location"=>"Galaxy Mall"),
		),
		);
		$o->objecttable($data,2);

	}
	function prospects(){
		$this->check_login();
		$this->data['objs']=Client::populate();
		$this->load->view('adm/prospects',$this->data);
	}
	function remove_image_path(){
		$params = $this->input->post();
		echo unlink($params['path']);
	}
	function removemaintenanceimage(){
		$params = $this->input->post();
		echo Maintenance_image::remove($params);
	}
	function requestupdate(){
		$params = $this->input->post();
		echo Ticket::updateticket();
	}
	function resetpasswordsendmail(){
		$params = $this->input->post();
		$result = $this->ion_auth->forgotten_password($params['email']);
		if($result){
			$message = "Anda menerima email ini karena anda meminta untuk mereset password anda pada aplikasi Teknis <br/ >";
			$message.= "Kode anda yang baru adalah : <br />" . $result['forgotten_password_code'] . '<br />';
			$message.= "Masukkan kode diatas pada tautan berikut : http://192.168.0.117/teknis/adm/entry_passwordcode";
			$message.= "<br /><br /><br />";
			$message.= "Best Regards";
			$message.= "<br /><br /><br />";
			$message.= "Admin Aplikasi PadiNET";
		}
		else
		{
			$message = 'Email anda tidak dikenali pada database kami';
		}
		echo $this->common->send_mail($params['email'],'DB Teknis :: Reset Password',$message);
	}
	function entry_passwordcode(){
		$this->load->view('adm/entry_passwordcode',$this->data);
	}
	function entry_code(){
		$params = $this->input->post();
		$kode = 'ade74f8b6857c8cc1db49768bfbe3eccf1a2e33c';
		$result = $this->ion_auth->forgotten_password_complete($params['kode']);
		if($result){
			$new_password = $result['new_password'];
			$identity = $result['identity'];
			$message = "Password baru anda : " . $new_password;
			echo $this->common->send_mail($result['identity'],'DB Teknis :: Password baru anda',$message);

		}
		else
		{
			$message = 'kode yang anda masukkan salah';
		}
	}
	function removeAll(){
		$query ="use teknis";

		$query.="delete from clients";
		$query.="alter table clients auto_increment=1";

		$query.="delete from client_applications";
		$query.="alter table client_applications auto_increment=1";

		$query.="delete from client_sites";
		$query.="alter table client_sites auto_increment=1";

		$query.="delete from install_sites";
		$query.="alter table install_sites auto_increment=1";

		$query.="delete from survey_sites";
		$query.="alter table survey_sites auto_increment=1";

		$query.="delete from survey_devices";
		$query.="alter table survey_devices auto_increment=1";

		$query.="delete from survey_images";
		$query.="alter table survey_images auto_increment=1";

		$query.="delete from survey_surveyors";
		$query.="alter table survey_surveyors auto_increment=1";

		$query.="delete from survey_gps_datas";
		$query.="alter table survey_gps_datas auto_increment=1";

		$query.="delete from survey_materials";
		$query.="alter table survey_materials auto_increment=1";

		$query.="delete from survey_requests";
		$query.="alter table survey_requests auto_increment=1";

		$query.="delete from survey_site_distances";
		$query.="alter table survey_site_distances auto_increment=1";

		$query.="delete from survey_requests_users";
		$query.="alter table survey_requests_users auto_increment=1";

		$query.="delete from survey_dates";
		$query.="alter table survey_dates auto_increment=1";

		$query.="delete from survey_client_distances";
		$query.="alter table survey_client_distances auto_increment=1";

		$query.="delete from survey_bts_distances";
		$query.="alter table survey_bts_distances auto_increment=1";

		$query.="delete from install_antennas";
		$query.="alter table install_antennas auto_increment=1";

		$query.="delete from install_ap_wifis";
		$query.="alter table install_ap_wifis auto_increment=1";

		$query.="delete from install_bas";
		$query.="alter table install_bas auto_increment=1";

		$query.="delete from install_dates";
		$query.="alter table install_dates auto_increment=1";

		$query.="delete from install_files";
		$query.="alter table install_files auto_increment=1";

		$query.="delete from install_images";
		$query.="alter table install_images auto_increment=1";

		$query.="delete from install_installers";
		$query.="alter table install_installers auto_increment=1";

		$query.="delete from install_materials";
		$query.="alter table install_materials auto_increment=1";

		$query.="delete from install_pccards";
		$query.="alter table install_pccards auto_increment=1";

		$query.="delete from install_requests";
		$query.="alter table install_requests auto_increment=1";

		$query.="delete from install_requests_users";
		$query.="alter table install_requests_users auto_increment=1";

		$query.="delete from install_routers";
		$query.="alter table install_routers auto_increment=1";

		$query.="alter table install_routers auto_increment=1";
		$query.="alter table install_routers auto_increment=1";

		$query.="delete from install_sites";
		$query.="alter table install_sites auto_increment=1";

		$query.="delete from install_wireless_radios";
		$query.="alter table install_wireless_radios auto_increment=1";

		$query.="delete from maintenance_images";
		$query.="alter table maintenance_images auto_increment=1";

		$query.="delete from maintenance_operators";
		$query.="alter table maintenance_operators auto_increment=1";

		$query.="delete from maintenance_pics";
		$query.="alter table maintenance_pics auto_increment=1";

		$query.="delete from maintenance_requests";
		$query.="alter table maintenance_requests auto_increment=1";

		$query.="delete from maintenance_requests_users";
		$query.="alter table maintenance_requests_users auto_increment=1";

		$query.="delete from maintenanceoperators";
		$query.="alter table maintenanceoperators auto_increment=1";

		$query.="delete from tickets";
		$query.="alter table tickets auto_increment=1";

		$query.="delete from troubleshoot_implementers";
		$query.="alter table troubleshoot_implementers auto_increment=1";

		$query.="delete from troubleshoot_requestrevisions";
		$query.="alter table troubleshoot_requestrevisions auto_increment=1";

		$query.="delete from troubleshoot_requests";
		$query.="alter table troubleshoot_requests auto_increment=1";

		$query.="delete from troubleshoot_sites";
		$query.="alter table troubleshoot_sites auto_increment=1";

		$query.="delete from troubleshootdevices";
		$query.="alter table troubleshootdevices auto_increment=1";

		$query.="delete from troubleshootsites";
		$query.="alter table troubleshootsites auto_increment=1";

		$query.="delete from altergrades";
		$query.="alter table altergrades auto_increment=1";

		$query.="delete from alterexecutors";
		$query.="alter table alterexecutors auto_increment=1";

		$query.="delete from site_routers";
		$query.="alter table site_routers auto_increment=1";

		$query.="delete from site_antennas";
		$query.="alter table site_antennas auto_increment=1";

		$query.="delete from site_ap_wifis";
		$query.="alter table site_ap_wifis auto_increment=1";

		$query.="delete from site_pccards";
		$query.="alter table site_pccards auto_increment=1";

		$query.="delete from site_wireless_radios";
		$query.="alter table site_wireless_radios auto_increment=1";

		$query.="delete from pics";
		$query.="alter table pics auto_increment=1";

		$query.="delete from client_priorities";
		$query.="alter table client_priorities auto_increment=1";


		$this->db->query($query);
	}
	function reset_password(){
		$this->load->view('adm/reset_password',$this->data);
	}
	function settings(){
		$this->check_login();
		$data = array(
			'csspath' => base_url() . 'css/aquarius/',
			'jspath' => base_url() . 'js/aquarius/',
			'imagepath' => base_url() . 'img/aquarius/',
			'user'=>User::get_user_by_id($this->user->id),
			'menuFeed'=>'settings'
		);
		$this->data['menuFeed'] = 'Settings';
		$this->load->view('adm/settings',$this->data);
	}
	function useradd(){
		$params = $this->input->post();
		echo $this->ion_auth->register($params['username'],$params['password'],$params['email'],array('fname'=>$params['fname'],'lname'=>$params['lname']));
	}
	function survey_result(){
		$this->check_login();
		$id = $this->uri->segment(3);
		$obj = Survey_request::get_obj_by_id($id);
		$this->data['obj']=$obj;
		$this->data['check0']=$obj->checkresume(0);
		$this->data['check1']=$obj->checkresume(1);
		$this->data['check2']=$obj->checkresume(2);
		$this->data['check3']=$obj->checkresume(3);
		$this->data['clients']=Client::get_combo_data();
		$this->data['hours'] = Common::gethours();
		$this->data['minutes'] = Common::getminutes();
		$this->data['sender']='survey_edit';
		$this->data['services'] = Service::get_combo_data();
		$this->load->view('adm/survey_result',$this->data);
	}
	function addsurveyofficer(){
		$params = $this->input->post();
		echo Survey_surveyor::add($params);
	}
	function addmaintenanceofficer(){
		$params = $this->input->post();
		echo Maintenance_operator::add($params);
	}
	function survey_dates(){
		$this->check_login();
		$id = $this->uri->segment(3);
		$obj = Survey_request::get_obj_by_id($id);
		$this->data['obj']=$obj;
		$this->data['check0']=$obj->checkresume(0);
		$this->data['check1']=$obj->checkresume(1);
		$this->data['check2']=$obj->checkresume(2);
		$this->data['check3']=$obj->checkresume(3);
		$this->data['clients']=Client::get_combo_data();
		$this->data['sender']='survey_dates';
		$this->load->view('adm/survey_dates',$this->data);

	}
	function surveyimage_remove(){
		$params = $this->input->post();
		echo Survey_image::remove($params['id']);
	}
	function survey_removeofficer(){
		$params = $this->input->post();
		echo Survey_surveyor::remove($params['id']);
	}
	function survey_removeotherclent(){
		$params = $this->input->post();
		Survey_client_distance::remove_client($params['id']);
	}
	function survey_update(){
		$params = $this->input->post();
		$params['survey_date'] = Common::human_to_sql_date($params['survey_date']);
		$message = "Permintaan survey ";
		$message.='PIC ' . $params['pic_name'];
		$message.='Telp. ' . $params['pic_phone'];
		$message.='Email ' . $params['pic_email'];
		$message.='Jabatan ' . $params['pic_position'];

		$this->common->send_mail('puji@padi.net.id','survey update',$message);
		echo Survey_request::request_update($params);
	}
	function surveyorupdate(){
		$params = $this->input->post();
		echo Survey_surveyor::edit($params);
	}
	function surveysitedistanceadd(){
		$params = $this->input->post();
		echo Survey_site_distance::add($params);
	}
	function surveysite_saveimage(){
		$params = $this->input->post();
		//rename('./media/surveys/' . $params['path'],'./media/surveys/' . $params['path']);
		echo Survey_site::saveimage($params);
	}
	function survey_site(){
		$this->check_login();
		$id = $this->uri->segment(3);
		$path = array(
			'csspath' => base_url() . 'css/aquarius/',
			'jspath' => base_url() . 'js/aquarius/',
			'imagepath' => base_url() . 'img/aquarius/',
			'user'=>User::get_user_by_id($this->user->id)
			);
		$data = array(
			'aps'=>AP::get_combo_data(),
			'device_type'=>Devicetype::get_combo_data(),
			'material_type'=>Materialtype::get_combo_data(),
			'obj'=>Survey_site::get_obj_by_id($id),
			'btstowers'=>Btstower::get_combo_data(),
			'clients'=>Client::get_combo_data(),
			'hours'=> Common::gethours(),
			'minutes'=> Common::getminutes(),
			'devices'=>Device::get_combo_data(),
			'path'=>$path,
			'sender'=>'survey_edit',
			'sites'=>Survey_site::get_other_sites($id),
		);
		$data = array_merge($data,$this->data);
		$this->load->view('adm/survey_site',$data);
	}
	function surveysitestoinstallsitescopy(){
		$params = $this->input->post();
		$survey = new Survey_site();
		$surveys = $survey->where('survey_request_id',$params['survey_id'])->get();
		foreach($surveys as $srv){
			$install = new Install_site();
			$install->install_request_id = $params['install_request_id'];
			$install->address = $srv->address;
			$install->city = $srv->city;
			$install->pic = $srv->pic;
			$install->position = $srv->position;
			$install->phone_area = $srv->phone_area;
			$install->phone = $srv->phone;
			$install->pic_email = $srv->pic_email;
			$install->description = $srv->description;
			$install->save();
		}
		echo $install->check_last_query();
	}
	function tables(){
		$data = array(
			'csspath' => base_url() . 'css/aquarius/',
			'jspath' => base_url() . 'js/aquarius/',
			'imagepath' => base_url() . 'img/aquarius/',
		);
		$this->load->view('adm/tables',$this->data);
	}
	function testnotification(){
		echo Install_request::get_notification('2');
	}
	function upload2(){
			$config['upload_path'] = './media/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			$config['max_size']	= '1000';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload())
			{
				$error = array('error' => $this->upload->display_errors());
				redirect(base_url() . 'adm/settings');
				//$this->load->view('adm/settings', $error);
			}
			$data = $this->upload->data();
			echo "<img src='" . base_url() . "media/". $data['file_name'] ."'  class='preview'>";
	 }
	function upload(){
		$path = base_url() . 'media/';
		$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
		if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
			{
				$name = $_FILES['photoimg']['name'];
				$size = $_FILES['photoimg']['size'];
				if(strlen($name))
					{
						 $ext = $this->getExtension($name);
						if(in_array($ext,$valid_formats))
						{
						if($size<(1024*1024))
							{
								$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
								$tmp = $_FILES['photoimg']['tmp_name'];
								if(move_uploaded_file($tmp, $path.$actual_image_name))
									{
									//mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");

										echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
									}
								else
									echo "Fail upload folder with read access.";
							}
							else
							echo "Image file size max 1 MB";
							}
							else
							echo "Invalid file format..";
					}
				else
					echo "Please select image..!";

				exit;
			}
	}
	function update_userpic(){
		$params = $this->input->post();
		echo User::update_userpic($params);
	}
	function updateinstallstatus(){
		$params = $this->input->post();
		echo Install_request::updatestatus($params);
	}
	function updatemaintenancestatus(){
		$params = $this->input->post();
		echo Maintenance_request::updatestatus($params);
	}
	function updatesurveystatus(){
		$params = $this->input->post();
		echo Survey_request::updatestatus($params);
	}
	function users(){
		$this->check_login();
		$data = array(
			'csspath' => base_url() . 'css/aquarius/',
			'jspath' => base_url() . 'js/aquarius/',
			'imagepath' => base_url() . 'img/aquarius/',
		);
		$this->load->view('adm/users',$this->data);
	}
	function upload_file(){
		$uploaddir = './media/';
		$file = $uploaddir . basename($_FILES['uploadfile']['name']);

		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
		  echo "success";
		} else {
			echo "error";
		}
	}
	function upload_clientimage(){
		$uploaddir = './media/clients/';
		$file = $uploaddir . basename($_FILES['uploadfile']['name']);

		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
		  echo "success";
		} else {
			echo "error";
		}
	}
	function upload_userimage(){
		$uploaddir = './media/users/';
		$file = $uploaddir . basename($_FILES['uploadfile']['name']);
		$file = $uploaddir . $this->session->userdata['username'] . '.jpg';
		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
			echo "success";
		} else {
			echo "error";
		}
	}
	function upload_surveyimage(){
		$uploaddir = './media/surveys/';
		$file = $uploaddir . basename($_FILES['uploadfile']['name']);
			if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
			echo "success";
		} else {
			echo "error";
		}
	}
	function upload_tmp(){
		$uploaddir = './media/tmp/';
		$file = $uploaddir . basename($_FILES['uploadfile']['name']);
		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
			echo "success";
		} else {
			echo "error";
		}
	}
	function upload_profile(){
		$uploaddir = './media/users/';
		$file = $uploaddir . strtolower($this->session->userdata["username"]).".jpg";
		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
			echo "success";
		} else {
			echo "error";
		}
	}
	function upload_ba(){
		$uploaddir = './media/installs/ba/';
		$file = $uploaddir . basename($_FILES['uploadfile']['name']);
		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
			echo "success";
		} else {
			echo "error";
		}
	}
	function userupdate(){
		$params = $this->input->post();
		echo User::edit($params);
	}
	function wifiadd(){
		$params = $this->input->post();
		echo Install_ap_wifi::add_aw($params);
	}
	function reports(){
		$id = $this->uri->segment(3);
		$obj = new Survey_request();
		$obj->where('id',$id)->get();
		$data = array('obj'=>$obj);
		$this->load->view('reports/survey',$data);
	}
	function sendemail(){
		$message = "";
		$message.= "Yth\n";
		$message.= "Puji P\n";
		$message.= "\n";
		$message.= "Di Tempat\n";
		echo $this->common->send_mail('puji@padi.net.id','[PadiNET App] DB Teknis',$message);
	}
	function mlm($id){
		$obj = new User();
		$obj->where('id',$id)->get();
		foreach($obj->user as $user){
			echo $user->username . '::' . $user->id . '<br />';
			$this->mlm($user->id);
		}
		$obj->close();
	}
	function callMlm(){
		$this->mlm(11);
	}
	function datatable(){
		$this->load->view("datatable/table");
	}
	function tableTemplate(){
		$this->load->view("adm/tableTemplate");
	}
	function setRole(){
		$params = $this->input->post();
		$session_data = array(
		    'role'=>$params["role"]
		);
		$this->session->set_userdata($session_data);		
	}
	function getRoles(){		
		$user = new User();
		$user->where('id',$this->session->userdata['user_id'])->get();
		echo $user->group->result_count() . '<br />';
		foreach($user->group as $role){
			echo $role->name . '<br />';
		}
	}
}
